package com.koron.device.service.propconfig;

import com.koron.device.bean.Equipment;
import com.koron.device.bean.EquipmentQuery;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Comparator;
import java.util.List;

/**
 * @author twcao
 * @title: DevicePropHandler
 * @projectName GDH_EAM_V2
 * @description: 设备属性值设置
 * @date 2022/2/1514:42
 */
@Component
public class DevicePropHandler {

    @Autowired
    private List<PropConfig> propConfigs;

    public void setUp(SessionFactory factory, List<Equipment> equipment, EquipmentQuery query) {
        if(CollectionUtils.isEmpty(equipment) || CollectionUtils.isEmpty(query.getConfigRules())) {
            return;
        }
        // 根据order从小到大排序
        propConfigs.stream()
                .filter(row -> CollectionUtils.isNotEmpty(query.getConfigRules()) && CollectionUtils.containsAny(query.getConfigRules(), row.sign()))
                .sorted(Comparator.comparingInt(PropConfig::getOrder))
                .forEach(row -> row.setup(factory, equipment, query));
    }
}
